package com.hippo.javacore.javaiostream.iostream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;

/**
 * @ClassName ObjectInputStreamOpt
 * @Description TODO 对象字节输入流操作类
 * @Author tangxl
 * @create 2023-04-07 10:58
 **/
public class ObjectInputStreamOpt {
    public static void main(String[] args) {
        try(
                // 创建一个对象字节输出流对象
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("study-commons/java-file-iostream/src/main/resources/file/a.txt"));
        ) {
            // 用一个学生对象来接收读取的对象
            ObjectOutputStreamOpt.Student s = (ObjectOutputStreamOpt.Student)ois.readObject();
            System.out.println(s);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    // 注意: 要想反序列化对象, 需要实现Serializable接口
    static class Student implements Serializable {
        int id;
        String name;
        int age;
        // transient关键字修饰的属性不会被序列化
        transient String password;
    }
}
